package com.csun.nusing.ui.jpush;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.jsonbean.AlarmDataJsonBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.activity.NursingAlarmAnswerActivity;
import com.csun.nusing.util.TTSUtils;
import com.csun.nusing.util.TimeUtils;
import com.google.gson.Gson;

import cn.jpush.android.api.JPushInterface;

/**
 * 接收推送的消息 ClassName: JPshMessageActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-3
 */
public class JPshMessageActivity extends BaseActivity<CommonPresenter> {

	private TextView statetv;
	private TextView timetv;
	private TextView placetv;
	private TextView phonetv;
	private Button clickbtn;
	private ImageView headimageView;
	private ImageView stateview;
	private String title;
	private String content;
	private String extra;
	private String mapdata;

	private String alertId;
	private String headpicture;
	private String oldname;
	private String creattime;
	private AlarmDataJsonBean databean;

	@Override
	public int getContentViewId() {
		return R.layout.item_nursing_alarm_list;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		initView();
	}

	private void initView() {
		statetv = findViewById(R.id.item_nursingalarm_list_state_tv);
		timetv = findViewById(R.id.item_nursingalarm_list_time_tv);
		placetv = findViewById(R.id.item_nursingalarm_list_place_tv);
		phonetv = findViewById(R.id.item_nursingalarm_list_phone_tv);
		clickbtn = findViewById(R.id.item_nursingalarm_list_button_btn);
		headimageView = findViewById(R.id.item_nursingalarm_list_head_iv);
		stateview = findViewById(R.id.item_nursingalarm_list_state_view);
		clickbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 点击跳转到消息处理界面
				Intent intent = new Intent(JPshMessageActivity.this,
						NursingAlarmAnswerActivity.class);
				intent.putExtra("AlertId", "" + alertId);
				intent.putExtra("AlertOlderName", "" + oldname);
				intent.putExtra("AlertOlderPic", "" + headpicture);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public void initData() {
		super.initData();
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
			content = bundle.getString(JPushInterface.EXTRA_ALERT);
			extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			Log.e("fzq", "--------" + title);
			Log.e("fzq", "--------" + content);
			//语音播报
			if (Integer.valueOf(android.os.Build.VERSION.RELEASE
					.substring(0, 1)) < 7) {
				TTSUtils.getInstance().speak(""+content);
			}
			Log.e("fzq", "--------" + extra);
			if (extra != null && extra.length() > 10) {
				// extra = extra.substring(35, extra.length() - 2); \ ""
				Gson gson = new Gson();
				databean = gson.fromJson(extra
						.substring(35, extra.length() - 2).replace("\\", ""),
						AlarmDataJsonBean.class);
				oldname = databean.getName();
				headpicture = databean.getHeadpicture();
				alertId = "" + databean.getAlertId();
				phonetv.setText("" + content);
				statetv.setText("" + oldname);
				placetv.setText("" + databean.getPhone());
				Glide.with(this).load("" + headpicture).into(headimageView);
				timetv.setText(""
						+ TimeUtils.millis2String(databean.getCreateTime()));
			}
		}
	}


}
